package com.ruoyi.web.controller.seal;

import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.entity.Certificate;
import com.ruoyi.common.core.domain.model.UploadCerBody;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.jm.CertificateInfo;
import com.ruoyi.common.utils.jm.CertificateUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.framework.config.ServerConfig;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.ICertificateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import static com.ruoyi.common.utils.Base64Utils.ESSGetBase64Decode;
import static com.ruoyi.common.utils.Base64Utils.ESSGetBase64Encode;
import static com.ruoyi.common.utils.PFXUtil.getX509Certificate;
import static com.ruoyi.common.utils.StringUtils.isNull;

/**
 * 证书信息Controller
 *
 * @author ess
 * @date 2021-10-18
 */
@RestController
@RequestMapping("/seal/certificate")
public class CertificateController extends BaseController
{
    @Autowired
    private ICertificateService certificateService;


    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询证书信息列表
     */
    @PreAuthorize("@ss.hasPermi('seal:certificate:list')")
    @GetMapping("/list")
    public TableDataInfo list(Certificate certificate)
    {
        startPage();
        List<Certificate> list = certificateService.selectCertificateList(certificate);
        return getDataTable(list);
    }

    /**
     * 导出证书信息列表
     */
    @PreAuthorize("@ss.hasPermi('seal:certificate:export')")
    @Log(title = "证书信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Certificate certificate)
    {
        List<Certificate> list = certificateService.selectCertificateList(certificate);
        ExcelUtil<Certificate> util = new ExcelUtil<Certificate>(Certificate.class);
        return util.exportExcel(list, "证书信息数据");
    }

    /**
     * 获取证书信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('seal:certificate:query')")
    @GetMapping(value = "/{certId}")
    public AjaxResult getInfo(@PathVariable("certId") String certId)
    {
        return AjaxResult.success(certificateService.selectCertificateByCertId(certId));
    }

    /**
     * 新增证书信息
     */
    @PreAuthorize("@ss.hasPermi('seal:certificate:add')")
    @Log(title = "证书信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Certificate certificate)
    {
        return toAjax(certificateService.insertCertificate(certificate));
    }

    /**
     * 修改证书信息
     */
    @PreAuthorize("@ss.hasPermi('seal:certificate:edit')")
    @Log(title = "证书信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Certificate certificate)
    {
        return toAjax(certificateService.updateCertificate(certificate));
    }

    /**
     * 删除证书信息
     */
    @PreAuthorize("@ss.hasPermi('seal:certificate:remove')")
    @Log(title = "证书信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{certIds}")
    public AjaxResult remove(@PathVariable String[] certIds)
    {
        return toAjax(certificateService.deleteCertificateByCertIds(certIds));
    }

    @ApiOperation("生成证书")
    @PostMapping("/getCertPair")
    public AjaxResult getCertPair(@RequestBody Certificate certificate) {
        return certificateService.createCertificate(certificate);
    }
    @ApiOperation("上传证书")
    @PostMapping("/upLoadCert")
    public AjaxResult upLoadCert(@RequestBody UploadCerBody uploadCerBody) {
        if(uploadCerBody==null){
            return AjaxResult.error("参数错误");
        }

        String commonUrl = RuoYiConfig.getUploadPath();
        return certificateService.upLoadCertificate(uploadCerBody,commonUrl);
    }
}
